<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>添加分类</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/admin/layuimini/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/admin/layuimini/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<form class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label required">分类名称</label>
        <div class="layui-input-block">
            <input type="text" name="name" lay-verify="required" lay-reqtext="分类名称不能为空" placeholder="请输入分类名称" value=""
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label required">分类简介</label>
        <div class="layui-input-block">
            <textarea name="introduction" lay-verify="required" lay-reqtext="分类简介不能为空" class="layui-textarea"
                      placeholder="请输入分类简介"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">是否前台显示</label>
        <div class="layui-input-block">
            <input type="radio" name="display" value="true" title="是" checked="">
            <input type="radio" name="display" value="false" title="否">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">颜色</label>
        <div class="layui-input-block">
            <div>
                <div id="color-select" lay-verify="colorVer"></div>
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="saveBtn">确认保存</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<script src="/static/admin/layuimini/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="/static/admin/xm-select/xm-select.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.19.2/axios.js"></script>
<script>
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;
        //渲染多选下拉框
        var colorSelect = xmSelect.render({
            el: '#color-select',
            language: 'zn',
            //开启搜索
            filterable: true,
            radio: true,
            clickClose: true,
            model: {
                label: {
                    type: 'text',
                    text: {
                        template: function (item, sels) {
                            return '<span>' + item.name + '</span>';
                        },
                    },
                }
            },
            template({name, value}) {
                return name + '<span style="position: absolute; right: 10px; background-color: ' + value + '">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>'
            },
            data: [
                {name: '#D5F5E3', value: '#D5F5E3'},
                {name: '#E8F8F5', value: '#E8F8F5'},
                {name: '#82E0AA', value: '#82E0AA'},
            ],
            create: function (val, arr) {
                if (arr.length === 0) {
                    return {
                        name: '创建' + val,
                        value: val,
                    }
                }
            },
        });
        //获取所有颜色
        $.ajax({
            url: '/admin/category/colors',
            method: 'GET',
            success: function (result) {
                let data = result.data.map(item => {
                    return {
                        name: item,
                        value: item
                    }
                });
                colorSelect.update({
                    data: data,
                    autoRow: true,
                })
            }
        });
        //表单验证
        form.verify({
            colorVer: function () {
                if (colorSelect.getValue('value').length <= 0) {
                    return '请选择颜色';
                }
                let value = colorSelect.getValue('value')[0];
                if (value[0] !== '#') {
                    return '颜色由#开头';
                }
                let pattern = /^#([0-9a-fA-F]{6})$/;
                if (!pattern.test(value)) {
                    return '颜色由#开头的6位16进制字符组成';
                }
            }
        });
        //监听提交
        form.on('submit(saveBtn)', function (data) {
            data.field.color = colorSelect.getValue('value')[0];
            axios({
                method: 'post',
                url: '/admin/category',
                data: data.field,
                headers: {
                    'X-Requested-With': 'XMLHttpRequest'
                },
            }).then((response) => {
                Swal.fire({
                    icon: 'success',
                    title: '保存成功',
                    showConfirmButton: false,
                    timer: 1500,
                    onClose: () => {
                        var parentLayer = parent.layer;
                        var iframeIndex = parentLayer.getFrameIndex(window.name);
                        parent.location.reload();
                        parentLayer.close(iframeIndex);
                    }
                });
            }).catch((error) => {
                Swal.fire({
                    icon: 'error',
                    title: '保存失败！',
                    text: `${error.response.data.message}`,
                });
            })
            return false;
        });
    });
</script>
</body>
</html>